/**
 * Mar 29, 2010
 */
package org.tomale.id.ui.preference.pages;

import java.util.List;

import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;
import org.tomale.id.ConnectionManager;
import org.tomale.id.common.Helper;
import org.tomale.id.common.KeyValuePair;
import org.tomale.id.ui.Activator;

/**
 * @author ferd
 *
 */
public class ConnectionPage extends PreferencePage implements
		IWorkbenchPreferencePage {

	Text _url;
	Text _user;
	Text _pw;
	Combo _cns;
	
	List<KeyValuePair<String, String>> _providers;
	
	/**
	 * 
	 */
	public ConnectionPage() {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	/**
	 * @param title
	 */
	public ConnectionPage(String title) {
		super(title);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	/**
	 * @param title
	 * @param image
	 */
	public ConnectionPage(String title, ImageDescriptor image) {
		super(title, image);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		l = new Label(comp, SWT.NONE);
		l.setText("Connection Provider");
		l.setLayoutData(new GridData());
		
		_cns = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
		_cns.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING,
				true,false));
		initProviders();
		
		l = new Label(comp, SWT.NONE);
		l.setText("Connection URL");
		l.setLayoutData(new GridData());
		
		_url = new Text(comp, SWT.BORDER);
		_url.setLayoutData(new GridData(SWT.FILL, 
				SWT.BEGINNING, true, false));
		
		l = new Label(comp, SWT.NONE);
		l.setText("Username");
		l.setLayoutData(new GridData());
		
		_user = new Text(comp, SWT.BORDER);
		_user.setLayoutData(new GridData(SWT.FILL,
				SWT.BEGINNING,true,false));
		
		l = new Label(comp, SWT.NONE);
		l.setText("Password");
		l.setLayoutData(new GridData());
		
		_pw = new Text(comp, SWT.BORDER | SWT.PASSWORD);
		_pw.setLayoutData(new GridData(SWT.FILL,
				SWT.BEGINNING,true,false));
		
		setPreferences();
		
		return comp;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {
		
	}

	@Override
	public boolean performOk() {
		try {
			if(valid()){
				save();
				return super.performOk();
			}
			return false;
		} catch(BackingStoreException e){
			setErrorMessage(e.getMessage());
			return false;
		}
	}
	
	private boolean valid(){
		// TODO perform validation
		return true;
	}
	
	private void save() throws BackingStoreException {
		IPreferenceStore store = getPreferenceStore();
		store.setValue(PreferenceConstants.CONNECTION_URL, _url.getText());
		store.setValue(PreferenceConstants.CONNECTION_USR, _user.getText());
		store.setValue(PreferenceConstants.CONNECTION_PW, _pw.getText());
		store.setValue(PreferenceConstants.CONNECTION_PID, 
				Helper.getPairByValue(_providers, _cns.getText()).getKey());
		
		IEclipsePreferences pref = new ConfigurationScope().getNode(Activator.PLUGIN_ID);
		pref.flush();
	}
	
	private void initProviders(){
		_providers = ConnectionManager.getConnectionProviders();
		for(KeyValuePair<String, String> kv : _providers){
			_cns.add(kv.getValue());
//			_cns.setData(kv.getKey(), kv);
		}
	}
	
	private void setPreferences(){
		IPreferenceStore store = getPreferenceStore();
		_url.setText(store.getString(PreferenceConstants.CONNECTION_URL));
		_user.setText(store.getString(PreferenceConstants.CONNECTION_USR));
		_pw.setText(store.getString(PreferenceConstants.CONNECTION_PW));
		if(!store.getString(PreferenceConstants.CONNECTION_PID).isEmpty()){
			_cns.setText(Helper.getPairByKey(_providers, store.getString(PreferenceConstants.CONNECTION_PID)).getValue());
		}
	}
	
//	private KeyValuePair<String, String> getPairByKey(final List<KeyValuePair<String, String>> pairs,
//		final String key){
//		for(KeyValuePair<String, String> kv : pairs){
//			if(key.equals(kv.getKey())){
//				return kv;
//			}
//		}
//		return null;
//	}
//	
//	private KeyValuePair<String, String> getPairByValue(final List<KeyValuePair<String, String>> pairs,
//		final String value){
//		for(KeyValuePair<String, String> kv : pairs){
//			if(value.equals(kv.getValue())){
//				return kv;
//			}
//		}
//		return null;
//	}
}
